﻿using B.S.ProcessEgine.API.Applocations.Commands.FormDetail;
using B.S.ProcessEgine.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.ProcessEgine.API.Controllers
{
    /// <summary>
    /// 表单明细管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FormDetailController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        IMediator mediator;
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<FormDetailController> logger;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志</param>
        public FormDetailController(IMediator mediator, ILogger<FormDetailController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }

        /// <summary>
        /// 表单明细新增
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> FormDetailSave(FormDetailSaveCommand command)
        {
            try
            {
                var result = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("添加表单明细出错" + ex.Message);
                return ApiResult.Fail("添加表单明细异常", StateCodeEnum.Exception);

            }
        }
        /// <summary>
        /// 查询表单明细
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetFormDetail([FromQuery] GetFormDetailCommand command)
        {
            try
            {
                var result = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("查询表单明细出错" + ex.Message);
                return ApiResult.Fail("查询表单明细异常", StateCodeEnum.Exception);

            }
        }


        /*  /// <summary>
          /// 表单字段明细新增
          /// </summary>
          /// <param name="request"></param>
          /// <returns></returns>
          [HttpPost]
          public async Task<ApiResult> FormDetailSave(IList<FromDetailRequestDto> s)
          {
              try
              {
                  FormDetailSaveCommand command = new FormDetailSaveCommand
                  {
                       FormDetails=s
                  };
                  var result = await mediator.Send(command);
                  return result;
              }
              catch (Exception ex)
              {
                  logger.LogError("添加表单明细出错" + ex.Message);
                  throw;
              }
          }*/

    }
}
